//
//  InformationViewController.m
//  Project_A Gary
//
//  Created by leilei on 16/3/23.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "InformationViewController.h"
#import "InformationCollectionViewCell.h"
#import "HeadCollectionViewCell.h"
#import "InformationModel.h"
#import "HeadModel.h"
#import "NewCarCollectionViewCell.h"
#import "EvaluateCollectionViewCell.h"
#import "ShoppingCollectionViewCell.h"
#import "DriveCarCollectionViewCell.h"
#import "TelevisionLiveCollectionViewCell.h"
#import "PictureCollectionViewCell.h"
#import "TitleView.h"
#import "EvaluateModel.h"
#import "DetailViewController.h"


// 在轮播图的时候签订scroll的代理协议
@interface InformationViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate,TitleViewDelegate>

// 主要内容的collectionView
@property (nonatomic, retain)UICollectionView *collectionV;



// 内容数组
@property (nonatomic, retain)NSMutableArray *dataArr;
// 轮播图数组
@property (nonatomic, retain)NSMutableArray *headArr;

@property (nonatomic, retain)NSArray *urlArr;

// 设置一个titleView的属性
@property (nonatomic, retain)TitleView *titleView;

@end

@implementation InformationViewController

// 释放
- (void)dealloc
{
    [super dealloc];
    [_collectionV release];
    [_dataArr release];
    [_headArr release];
    [_titleView release];
    [_urlArr release];
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 防止内置图片混乱
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 导航栏设置
    self.navigationItem.title = @"太平洋汽车";
    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(click)];
    
    
    

    // 解析数据
    [self dataHandle];

#pragma mark - 创建titleView
    //创建titleView
    TitleView *titleView = [TitleView creatTitleView];
    [self.view addSubview:titleView];
    self.titleView = titleView;
    //代理5步 设置代理人
    self.titleView.delegates = self;
    // 建立一个标签的数组
    titleView.titleArray = @[@"首页", @"新车", @"评测", @"导购", @"用车", @"直播", @"图赏"];
   
    // 创建主要内容(嵌套)
    [self createCollectionView];
    
   
}



#pragma mark - 建立主要内容,collectionView与tableview嵌套
- (void)createCollectionView
{
    // 设置瀑布流
    UICollectionViewFlowLayout *FlowLay = [[UICollectionViewFlowLayout alloc] init];
    FlowLay.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    FlowLay.minimumLineSpacing = 0;
    FlowLay.itemSize = CGSizeMake(BOUNDS_WIDTH, BOUNDS_HEIGHT - 64 - 40 - 49);
    
    
    //建立collectionView
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 + 40 , BOUNDS_WIDTH, BOUNDS_HEIGHT - 64 - 40 - 49 ) collectionViewLayout:FlowLay];
    self.collectionV.backgroundColor = [UIColor redColor];
    // 允许整页滑动
    self.collectionV.pagingEnabled = YES;
    //隐藏滑动小进度条
    self.collectionV.showsHorizontalScrollIndicator = YES;
    
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.view addSubview:self.collectionV];
    
    
    
    
    // "首页"注册重用池
    [self.collectionV registerClass:[InformationCollectionViewCell class] forCellWithReuseIdentifier:@"InformationCollectionViewCell"];
    // "新车"注册重用池
    [self.collectionV registerClass:[NewCarCollectionViewCell class] forCellWithReuseIdentifier:@"NewCarCollectionViewCell"];
    // "探测"
    [self.collectionV registerClass:[EvaluateCollectionViewCell class] forCellWithReuseIdentifier:@"EvaluateCollectionViewCell"];
    

    // 导购
    [self.collectionV registerClass:[ShoppingCollectionViewCell class] forCellWithReuseIdentifier:@"ShoppingCollectionViewCell"];


    // 用车
    [self.collectionV registerClass:[DriveCarCollectionViewCell class] forCellWithReuseIdentifier:@"DriveCarCollectionViewCell"];

    // 直播
    [self.collectionV registerClass:[TelevisionLiveCollectionViewCell class] forCellWithReuseIdentifier:@"TelevisionLiveCollectionViewCell"];

    // 鉴赏
    [self.collectionV registerClass:[PictureCollectionViewCell class] forCellWithReuseIdentifier:@"PictureCollectionViewCell"];

    
    
    // 释放
    [FlowLay release];
    [_collectionV release];

}



#pragma mark - 解析数据
- (void)dataHandle
{
    
    
    [NetworkHandle GetDataWithURL:@"http://mrobot.pcauto.com.cn/v2/cms/channel1?pageNo=1&pageSize=20&serialIds=&chId=wandoujia" dic:nil success:^(id responseObject) {
        
        self.dataArr = [InformationModel transformWithArray:responseObject[@"data"]];
        
        self.headArr = [HeadModel transformWithArray:responseObject[@"focus"]];
        
        
        [self.collectionV reloadData];

        
    } filed:^(NSError *error) {
           }];
    
    
    
}

#pragma mark - 设置titleView的两个方法
//滚动结束减速
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / BOUNDS_WIDTH;
    [self.titleView setSelectedIndex:page];

}

#pragma mark - titleView的必须写的方法
//****titleView协议方法(代理第六步)
-(void)titleView:(TitleView *)title selectedIndexChanged:(NSInteger)index
{

    [self.collectionV scrollRectToVisible:CGRectMake(index * BOUNDS_WIDTH, 0, BOUNDS_WIDTH, BOUNDS_HEIGHT) animated:YES];
}



#pragma mark - collectionView两个必用的方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 7;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    
        if (0 == indexPath.item) {
        
        InformationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"InformationCollectionViewCell" forIndexPath:indexPath];
        
        cell.Arr = self.dataArr;
            if (self.headArr) {
                cell.HeadArr = self.headArr;
            }
            // 把vc的navi传递给cell的navi
        cell.navi = self.navigationController;
            
            
        return cell;
    }else if (1 == indexPath.item){
        NewCarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewCarCollectionViewCell" forIndexPath:indexPath];
        cell.navi = self.navigationController;


        return cell;
    }else if (2 == indexPath.item){
        EvaluateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EvaluateCollectionViewCell" forIndexPath:indexPath];
        cell.navi = self.navigationController;

        return cell;
        
    }else if (3 == indexPath.item){
        ShoppingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShoppingCollectionViewCell" forIndexPath:indexPath];
        cell.navi = self.navigationController;

        return cell;
    }else if (4 == indexPath.item){
        DriveCarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DriveCarCollectionViewCell" forIndexPath:indexPath];
        cell.navi = self.navigationController;

        return cell;
        //用
    }else if (5 == indexPath.item){
        TelevisionLiveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TelevisionLiveCollectionViewCell" forIndexPath:indexPath];
        cell.navi = self.navigationController;

        return cell;
        // 用
    }else  {
        PictureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PictureCollectionViewCell" forIndexPath:indexPath];
        cell.navi = self.navigationController;

        return cell;
    
 }
}

#pragma mark - 跳转到这么页面的时候 导航栏变为蓝色
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
}

#pragma mark - collectionView点击方法
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    DetailViewController *detailVC = [[DetailViewController alloc] init];
//    
//    
//    [self.navigationController pushViewController:detailVC animated:YES];
//}



#pragma mark - 左搜索按钮方法
- (void)click
{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
